#include "StdAfx.h"
#include "Statistics.h"
#include <math.h>

Statistics::Statistics(void)
{
	length = 0;
	max = 0;
	average = 0;
	stdDeviation = 0;

}

Statistics::~Statistics(void)
{
}

void Statistics::Calculate(short* sample, int length, bool useAbsoluteValues)
{
	this->length = length;
	long total = 0;
	for(int i=0; i<length; i++)
	{
		short val = useAbsoluteValues ? abs(sample[i]) : sample[i];
		total += val;
		if(val>max)
			max = val;
	}
	average = (double)total/(double)length;

	double squaredSum = 0;
	for(int i=0; i<length; i++)
	{
		double val = (double) (useAbsoluteValues ? abs(sample[i]) : sample[i]);
		squaredSum += pow( (val-average), 2);
	}
	stdDeviation = sqrt(squaredSum/length);
}

void Statistics::GetStatistics(short* sample, int length, std::vector<Statistics>& receiver, int sliceLength, bool useAbsoluteValues)
{
	for(int i=0; i<length; i+=sliceLength)
	{
		Statistics stats;
		stats.Calculate(sample+i, sliceLength, useAbsoluteValues);
		receiver.push_back(stats);
	}
}